import axios from 'axios';
import { Message } from 'element-ui';


// 创建axios实例
const service = axios.create({
  baseURL: process.env.BASE_API,
  timeout: 15000, // 请求超时时间
  withCredentials: true, // 支持跨域时发送 Cookie
});


// respone拦截器
service.interceptors.response.use(
  (response) => {
    const res = response.data;

    if (!res.success) {
      Message({
        message: res.errMessage,
        type: 'error',
        duration: 5 * 1000,
      });

      return Promise.reject('error');
    }

    return res;
  },
  (error) => {
    // console.log(`Error: ${error}`);

    Message({
      message: error.message,
      type: 'error',
      duration: 5 * 1000,
    });

    return Promise.reject(error);
  },
);

export default service;
